<?php

namespace App\Service\User;

use App\Model\PayOrder;
use App\Model\Service\CacheService;
use App\Model\UserProfile;

/**
 * 用户列表
 */
class UserListService extends UserBaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        //hot-精选，recharge-最新充值，new-最新注册，all-全部
        $type = $this->request->input('type', 'hot');
        $page = $this->request->input('page', 1);
        $userIds = $this->getUserList($type);
        $start = ($page - 1) * $this->pageNum;
        $userIds = array_slice($userIds, $start, $this->pageNum);
        $usersInfo = UserProfile::getUsersProfile($userIds, false, true);
        $ret = ['list' => []];
        array_walk($usersInfo, function ($userProfile) use (&$ret) {
            $item = UserProfile::buildUserItem($userProfile);
            $ret['list'][] = $item;
        });
        $this->packPhotos($ret['list'], $userIds);
        // log_user_result($userId, $userIds);
        return $ret;
    }

    /**
     * 获取用户列表（不同类型）
     */
    function getUserList($type = 'hot')
    {
        switch ($type) {
            case 'hot':
                $userIds = PayOrder::getRechargeUsers(1);
                $userIds = array_diff($userIds, CacheService::invalidUsers());
                break;
            case 'recharge':
                $userIds = PayOrder::getRechargeUsers(1);
                $userIds = array_diff($userIds, CacheService::invalidUsers());
                break;
            case 'new':
                $userIds = UserProfile::getNewUsers(UserProfile::ROLE_USER, 1);
                break;
            case 'all':
                $userIds = UserProfile::getNewUsers(UserProfile::ROLE_USER, 1);
                break;
        }
        return $userIds;
    }
}
